﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using HinduShop.DbClassLib;
using HinduShop.Domain.Models;
using HinduShop.Domain.Models.Account;
using Ninject;

namespace HinduShop.UI
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null
                       ? null
                       : (IController) ninjectKernel.Get(controllerType);
        }

        private void AddBindings()
        {
            ninjectKernel.Bind<IRepository<Product>>()
                .To<ModelsRepository<Product>>();

            ninjectKernel.Bind<IRepository<Category>>()
                .To<ModelsRepository<Category>>();

            ninjectKernel.Bind<IUnitOfWorkFactory>()
                .To<UnitOfWorkFactory>();

            ninjectKernel.Bind<IRepository<User>>()
                .To<ModelsRepository<User>>();

            ninjectKernel.Bind<IAuthProvider>()
                .To<FormsAuthProvider>();
        }
    }
}